package com.inspur.edp.common.expr.expresses.field;


import com.inspur.edp.common.expr.Express;
import com.inspur.edp.common.expr.ExpressDSLGenerator;
import com.inspur.edp.common.expr.ast.ASTContext;
import com.inspur.edp.common.type.TypeRefer;
import com.inspur.edp.common.type.utils.TypeUtil;

/**
 * @author lizhaorui
 * @description 轻量的字段访问表达式 light weight
 * @date 2025/5/20 9:29
 */
public class LwFieldAccessDSL implements ExpressDSLGenerator {

    @Override
    public String getKind() {
        return LwFieldAccessExpr.KIND;
    }

    @Override
    public String generate(ASTContext context, Express express) {
        LwFieldAccessExpr fieldAccessNewExpr = (LwFieldAccessExpr) express;
        StringBuilder sb = new StringBuilder();
        TypeRefer rootType = fieldAccessNewExpr.getRootType();
        int index = 0;
        for (LwFieldAccessExpr.FieldItem fieldItem : fieldAccessNewExpr.getItems()) {
            if (index > 0) {
                sb.append(".");
            }
            if (index == 0 && rootType != null) {
                String rootTypeUrl = TypeUtil.getTypeUrl(rootType);
                context.addImport(rootTypeUrl);
                String typeCode = TypeUtil.getTypeCode(rootTypeUrl);
                sb.append(typeCode);
            } else {
                sb.append(fieldItem.getCode());
            }
            index++;
        }
        return sb.toString();
    }
}
